package com.rcache.core.netty;


import com.distributed.netty.client.Client;
import com.distributed.netty.entity.ElementJob;
import com.distributed.netty.entity.JobType;
import com.distributed.netty.server.Server;
import com.rcache.core.Element;
import com.rcache.core.cache.BaseCache;
import com.rcache.core.cache.Rcache;
import com.rcache.core.distributed.IDistributedAgency;
import com.rcache.core.exception.ConfigurationException;
import com.rcache.core.manager.CacheManager;
import org.apache.log4j.Logger;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @desc 分布式代理
 * @author gzy
 * @time 2022-06-04
 */
public class DistributedAgency implements IDistributedAgency {
//    private
    private Server server;
    private Client client;
    private boolean master=false;
    static ConcurrentHashMap<String,DistributedAgency> concurrentHashMap=new ConcurrentHashMap();
    static Logger LOG=Logger.getLogger(DistributedAgency.class);

    public static DistributedAgency getInstance(String cacheName) throws ConfigurationException {
        Rcache cache = CacheManager.newInstance().getCache(cacheName);
        DistributedAgency agency = getInstance((BaseCache) cache);
        agency.master=true;
        return agency;
    }

    public static DistributedAgency getInstance(Rcache cache) {
        //获取本机ip
        InetAddress inet4Address = null;
        try {
            inet4Address = Inet4Address.getLocalHost();
            String ipSelf = inet4Address.toString().split("/")[1];
            DistributedAgency agency =  concurrentHashMap.get(cache.getCacheName());
            if (agency==null){
                agency=new DistributedAgency();
                if (cache.getDistributeIP().equals(ipSelf)){
                    LOG.debug("本机是master 开启master 服务");
                    //服务端对象
                    agency.server=new Server(cache);
                    agency.master=true;
                    agency.server.start();

                }else{
                    LOG.debug("本机是slave 开启slave 服务");
                    //客户端对象
                    agency.client=new Client(cache,cache.getDistributeIP());
                    agency.client.start();
                }
                concurrentHashMap.put(cache.getCacheName(),agency);
            }
            return agency;
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @desc 添加元素
     */
    public void put(Object[] args){
        if (args!=null&&args.length>0){
//            Element element=new Element(args[0],args[1]);
            ElementJob elementJob=new ElementJob(JobType.PUT,args,"执行添加");
            //如果本机是master
            if (this.master){
                this.server.addJob(elementJob);
            }else {
                this.client.addJob(elementJob);
            }
        }
    }

    /**
     * @desc 更新元素
     * @param args
     */
    public void replace(Object[] args) {
        if (args!=null&&args.length>0){
//            System.out.println("+++++_)__"+args[1]);
            ElementJob elementJob=new ElementJob(JobType.UPDATE,args,"执行更新");
            //如果本机是master
            if (this.master){
                this.server.addJob(elementJob);
            }else {
                this.client.addJob(elementJob);
            }
        }
    }

    /**
     * @desc 删除元素
     * @param args
     */
    public void delete(Object[] args) {
        if (args!=null&&args.length>0){
            ElementJob elementJob=new ElementJob(JobType.DELETE,args,"执行删除");
            //如果本机是master
            if (this.master){
                this.server.addJob(elementJob);
            }else {
                this.client.addJob(elementJob);
            }
        }
    }
}
